//
//  GYOrderInfoTableViewCell.m
//  GoodLuck
//
//  Created by 郭为勇 on 2018/3/22.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYOrderInfoTableViewCell.h"
#import "GYOrderStore.h"
#import "GYOrderDetailCollectionViewCell.h"

static NSString *orderDetailsCollectionCellIdentifier = @"orderDetailsCollectionCellIdentifier";

@interface GYOrderInfoTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIAlertViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *storeNameLable;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionViewHeightCons;
@property (weak, nonatomic) IBOutlet UILabel *orderFreightLable;
@property (weak, nonatomic) IBOutlet UILabel *realPayPriceLable;
@property (weak, nonatomic) IBOutlet UILabel *markLbale;

/** 商品 */
@property (nonatomic, strong) NSArray *goodsList;


@end
@implementation GYOrderInfoTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    GYOrderInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:orderDetailsCollectionCellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] firstObject];
    }
    
    return cell;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    //设置collectionView的一些属性
    [self.collectionView registerNib:[UINib nibWithNibName:@"GYOrderDetailCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:orderDetailsCollectionCellIdentifier];
}

- (IBAction)onClick:(UIButton *)sender {
    switch (sender.tag) {
        case 1:
            {
                self.customerServerCallBack();
            }
            break;
            
        case 2:
            {
                if (self.orderDetails.store_phone != nil && self.orderDetails.store_phone.length != 0) {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",self.orderDetails.store_phone]]];
                }else {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"该商家还没有设置联系方式!" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                    [alert show];
                }
            }
            break;
            
        default:
            break;
    }
}

- (void)setOrderDetails:(GYOrderDetails *)orderDetails {
    _orderDetails = orderDetails;
    
    //赋值
    
    self.storeNameLable.text = orderDetails.store_name;
    self.orderFreightLable.text = [NSString stringWithFormat:@"¥%@",orderDetails.shipping_fee];
    self.realPayPriceLable.text = [NSString stringWithFormat:@"¥%@",orderDetails.real_pay_amount];
    
    self.goodsList = orderDetails.goods_list;
    //计算collectionView的高度
    GYOrderDetailCollectionViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"GYOrderDetailCollectionViewCell" owner:nil options:nil] firstObject];
    cell.order_state = orderDetails.order_state;
    self.collectionViewHeightCons.constant = [cell getHeight:self.goodsList[0]]*self.goodsList.count + (self.goodsList.count-1)*10;
    
    //刷新表格数据;
    [self.collectionView reloadData];
}

- (CGFloat)getHeight:(GYOrderDetails *)model {
    self.orderDetails = model;
    
    [self layoutIfNeeded];
    
    return CGRectGetMaxY(self.markLbale.frame)+ 10;
}

#pragma mark -- UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.goodsList.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GYOrderDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:orderDetailsCollectionCellIdentifier forIndexPath:indexPath];
    cell.orderGoods = self.goodsList[indexPath.item];
    cell.order_state = self.orderDetails.order_state;
    cell.if_lock = self.orderDetails.isIf_lock;

    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    GYOrderDetailCollectionViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"GYOrderDetailCollectionViewCell" owner:nil options:nil] firstObject];
    cell.order_state = self.orderDetails.order_state;
    //NSLog(@"-------%f",[cell getHeight:self.goodsList[indexPath.item]]);
    return CGSizeMake(self.width, [cell getHeight:self.goodsList[indexPath.item]]);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}



#pragma mark -- UIAlertViewDelegate

//- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
//    if (buttonIndex == 1) {
//       [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",self.orderDetails.store_phone]]];
//    }
//}






@end
